﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MisClient
{
    public partial class FrmStatement : BaseForm
    {
        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();
        private IBLL.IType typeTjBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteType();

        private int dataGridView = 0;

        public FrmStatement()
        {
            InitializeComponent();
        }

        private void FrmStatement_Load(object sender, EventArgs e)
        {
            this.openStarDate.Value = DateTime.Now.Date.AddDays(-10);
            this.openStarDate.Focus();
        }

        private void txtDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
                if (depart == null)
                {
                    MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                this.txtBoxDep.Text = depart.DepName;
                this.comboBox4.Focus();
            }
        }

        private void txtType1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtType1.Text.Trim()))
                {
                    MisModel.Type type = typeTjBLL.GetType1ById(this.txtType1.Text.Trim());
                    if (type == null)
                    {
                        MessageBox.Show("统计类型不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtType1.Focus();
                        return;
                    }
                    else
                        this.textBox29.Text = type.TypeName;
                }
                this.txtType2.Focus();
            }
        }

        private void txtType2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtType2.Text.Trim()))
                {
                    MisModel.Type type = typeTjBLL.GetType1ById(this.txtType2.Text.Trim());
                    if (type == null)
                    {
                        MessageBox.Show("统计类型不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtType2.Focus();
                        return;
                    }
                    else
                        this.textBox31.Text = type.TypeName;
                }
                this.txtType3.Focus();
            }
        }

        private void txtType3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtType3.Text.Trim()))
                {
                    MisModel.Type type = typeTjBLL.GetType1ById(this.txtType3.Text.Trim());
                    if (type == null)
                    {
                        MessageBox.Show("统计类型不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtType3.Focus();
                        return;
                    }
                    else
                        this.textBox5.Text = type.TypeName;
                }
                this.btnOk.Focus();
            }
        }

        private void comboBox4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBox1.Focus();
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtType1.Focus();
            }
            
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void btnPri_Click(object sender, EventArgs e)
        {
            this.printPreviewDialog1.Document = this.printDocument1;
            this.printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;
            this.printPreviewDialog1.ShowDialog();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            string _Title = "对 帐 单";
            Font _TitleFont = new Font("宋体", 20, FontStyle.Bold);
            Point _CurrentPoint = new Point(30, 30);
            _CurrentPoint.X = (e.PageBounds.Width) / 2 - (int)(e.Graphics.MeasureString(_Title, _TitleFont).Width) / 2;
            e.Graphics.DrawString(_Title, _TitleFont, new SolidBrush(Color.Black), _CurrentPoint);
            StringBuilder sb = new StringBuilder();
            sb.Append("\n");
            sb.Append(string.Format("日期：{0} 至 {1}\n", this.openStarDate.Value.ToShortDateString(), this.openEndDate.Value.ToShortDateString()));
            sb.Append(string.Format("部门：{0}\n", this.txtDepCode.Text.Trim()));
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append("        收  入  项  目             金  额             支  出  项  目             金  额\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            if (this.dataGridView1.Rows.Count > 0)
            {
                DataGridViewRow dr = null;
                for (int i = dataGridView; i < this.dataGridView1.RowCount; i++, dataGridView++)
                {
                    /*dr = this.dataGridView1.Rows[i];
                    sb.Append(dr.Cells[0].Value.ToString()).Append(" ");
                    sb.Append(dr.Cells[1].Value.ToString()).Append(" ");
                    sb.Append(dr.Cells[2].Value.ToString());
                    sb.Append(getLenStr(8, decimal.Parse(dr.Cells[3].Value.ToString()).ToString("f2")));
                    sb.Append(getLenStr(8, decimal.Parse(dr.Cells[4].Value.ToString()).ToString("f2")));
                    sb.Append(getLenStr(8, decimal.Parse(dr.Cells[5].Value.ToString()).ToString("f2")));
                    sb.Append(getLenStr(9, decimal.Parse(dr.Cells[6].Value.ToString()).ToString("f2")));
                    sb.Append(getLenStr(8, decimal.Parse(dr.Cells[7].Value.ToString()).ToString("f2")));
                    sb.Append(getLenStr(10, decimal.Parse(dr.Cells[8].Value.ToString()).ToString("f2"))).Append("\n");
                    if (i != 0 && i % 44 == 0)
                    {
                        dataGridView++;
                        break;
                    }*/
                }
            }
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("制表：{0} 姓名{1}      日期：{2}    注：如有进价误差或销价误差，速与计算机科联系。", accountUser.UserCode,accountUser.Oper, DateTime.Now));

            _CurrentPoint.X = 30;
            _CurrentPoint.Y += (int)(e.Graphics.MeasureString(_Title, _TitleFont).Height) + 20;
            Font _ContentFont = new Font("宋体", 12);
            e.Graphics.DrawString(sb.ToString(), _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            if (dataGridView < this.dataGridView1.RowCount - 1)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
        }

        private String getLenStr(int len, string str)
        {
            if (null != str && !"".Equals(str))
            {
                int tempLen = str.Length;
                if (tempLen < len)
                {
                    StringBuilder sb = new StringBuilder();
                    tempLen = len - tempLen;
                    for (int i = 0; i < tempLen; i++)
                    {
                        sb.Append(" ");
                    }
                    str = sb.Append(str).ToString();
                }
            }
            return str;
        }

    }
}
